package com.wxd.controller;

import com.wxd.entity.ActionResult;
import com.wxd.entity.User;
import com.wxd.enums.ResultCodeEnum;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Hello Spring Boot 2.0!";
    }

    @PostMapping("/test")
    public String test() {
        return "Hello World";
    }

    @PostMapping("/error")
    public ActionResult error() {
        return ActionResult.defaultFail(ResultCodeEnum.RC500);
    }

    @PostMapping("/exception")
    public ActionResult exception() {
        int i = 1 / 0;
        return ActionResult.defaultFail(ResultCodeEnum.RC500);
    }

    @PostMapping("/getUser")
    public ActionResult getUser() {
        User user = new User();
        user.setId(UUID.randomUUID().toString());
        user.setName("MrDong");
        user.setAge(20);
        return ActionResult.defaultOk(user);
    }


    @PostMapping("/getUser2")
    public Object getUser2(String id) {
        User user1 = new User();
        user1.setId("1");
        user1.setName("张三");
        user1.setAge(20);
        User user2 = new User();
        user2.setId("2");
        user2.setName("李四");
        user2.setAge(25);
        Map<String, User> map = new HashMap<>();
        map.put("1", user1);
        map.put("2", user2);
        return map.get(id);
    }

    @PostMapping("/addUser")
    public void addUser(@RequestBody User user) {
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getAge());
    }
}